<template>
  <div
    class="relative h-screen bg-white dark:bg-zinc-800 text-center xl:bg-zinc-200"
  >
    <!-- 头部 -->
    <headerVue />
    <!-- 表单区 -->
    <div
      class="block px-3 mt-4 dark:bg-zinc-800 xl:bg-white xl:w-[388px] xl:dark:bg-zinc-900 xl:m-auto xl:mt-8 xl:py-4 xl:rounded-sm xl:shadow-lg"
    >
      <h3
        class="hidden mb-2 font-semibold text-base text-main dark:text-zinc-300 xl:block"
      >
        注册账号
      </h3>
      <!-- 登录表单 -->
      <vee-form @submit="onRegisterHandler">
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-none pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default::bg-zinc-900"
          type="text"
          name="username"
          placeholder="用户名"
          autocomplete="on"
          :rules="validateUsername"
          v-model="registerForm.username"
        />
        <vee-error-message
          name="username"
          class="text-red-600 text-sm block mt-0.5 text-left"
        />
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-none pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default::bg-zinc-900"
          type="password"
          name="password"
          placeholder="密码"
          autocomplete="on"
          :rules="validatePassword"
          v-model="registerForm.password"
        />
        <vee-error-message
          name="password"
          class="text-red-600 text-sm block mt-0.5 text-left"
        />
        <vee-field
          class="dark:bg-zinc-800 dark:text-zinc-400 border-b-zinc-400 border-b w-full outline-none pb-1 px-1 text-base focus:border-b-main dark:focus:border-b-zinc-200 xl:default::bg-zinc-900"
          type="password"
          name="confirmPassword"
          placeholder="确认密码"
          autocomplete="on"
          rules="validateConfirmPassword:@password"
          v-model="registerForm.confirmPassword"
        />
        <vee-error-message
          name="confirmPassword"
          class="text-red-600 text-sm block mt-0.5 text-left"
        />
        <!-- 跳转按钮 -->
        <div class="pt-1 pb-3 leading-[0px] text-right">
          <a
            class="inline-block p-1 text-zinc-400 text-right dark:text-zinc-600 hover:text-zinc-600 dark:hover:text-zinc-400 text-sm duration-300 cursor-pointer"
            @click="onToLogin"
          >
            去登录
          </a>
        </div>
        <!-- 登录按钮 -->
        <div class="text-center">
          <a
            class="text-zinc-400 dark:text-zinc-600 hover:text-zinc-600 dark:hover:text-zinc-400 text-sm duration-300 cursor-pointer"
            target="__blank"
            href="https://m.imooc.com/newfaq?id=89"
            >注册即同意《用户注册协议》</a
          >
        </div>

        <!-- 注册按钮 -->
        <z-button
          class="w-full dark:bg-zinc-900 xl:dark:bg-zinc-800"
          :isActiveAnim="false"
          :loading="loading"
          >立即注册</z-button
        >
      </vee-form>
    </div>
  </div>
</template>

<script setup>
import headerVue from '../components/header.vue'
import {
  Form as VeeForm,
  Field as VeeField,
  ErrorMessage as VeeErrorMessage,
  defineRule
} from 'vee-validate'
import {
  validateUsername,
  validatePassword,
  validateConfirmPassword
} from '../validate'
import { LOGIN_TYPE } from '@/constants'
import { ref } from 'vue'
import { useUserStore } from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { useAppStore } from '@/store/modules/app'

const userStore = useUserStore()
const appStore = useAppStore()
const router = useRouter()
const route = useRoute()

const registerForm = ref({
  username: '',
  password: '',
  confirmPassword: ''
})

/**
 * 插入规则
 */
defineRule('validateConfirmPassword', validateConfirmPassword)

const loading = ref(false)
/**
 * 注册表单
 */
const onRegisterHandler = async () => {
  loading.value = true
  try {
    const payload = {
      username: registerForm.value.username,
      password: registerForm.value.password,
      ...route.query
    }
    await userStore.register(payload)
    // 注册成功，去登录
    await userStore.login({
      ...payload,
      type: LOGIN_TYPE.USERNAME
    })
  } finally {
    loading.value = false
  }
}

/**
 * 去登录
 */
const onToLogin = () => {
  appStore.changeRouterType('push')
  router.push({
    path: '/login'
  })
}
</script>

<style lang="scss" scoped></style>
